package com.tjetc.view;

import com.tjetc.domain.Park;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.ParkService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class ParkManageView {
    private static Scanner sc=new Scanner(System.in);
    private ParkService parkService= ClientProxy.getProxyInstance(ParkService.class);
    public void start() {
        while (true){
            System.out.println("1.查询园区");
            System.out.println("2.添加园区");
            System.out.println("3.删除园区");
            System.out.println("4.修改园区");
            System.out.println("0.返回上一级");
            System.out.println("请输入你的选择(0~4)");
            int i = sc.nextInt();
            switch (i){
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    del();
                    break;
                case 4:
                    update();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误请重新输入");
            }
        }
    }

    private void update() {
        list();
        System.out.println("请输入你想要更新的园区id");
        int id= sc.nextInt();
        System.out.println("请输入修改后的园区地址");
        String description= sc.next();
        Result result=parkService.update(new Park(id,description));
        System.out.println(result.getMsg());
    }

    private void del() {
        list();
        System.out.println("请输入想要删除的id");
        int id= sc.nextInt();
        Result result=parkService.delete(id);
        System.out.println(result.getMsg());
    }

    private void add() {
        System.out.println("请输入园区地址");
        String description;
        while (true) {
            description= sc.next();
            Result result=parkService.getByDescription(description);
            if (result.getCode()==200){
                System.out.println("该园区地址已经存在,请重新输入");
            }
            else {
                break;
            }
        }
        Park park=new Park();
        park.setDescription(description);
        Result result=parkService.add(park);
        System.out.println(result.getMsg());

    }

    public boolean list() {
        Result result=parkService.list();
        List<Park> list=(List<Park>) result.getData();
        if (!list.isEmpty()){
            for (Park park : list) {
                System.out.println(park);
            }
            return true;
        }else {
            System.out.println("没有园区呢");
            return false;
        }
    }
}
